-module(branch).

-include("records.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("stdlib/include/qlc.hrl").

-export([new/3]).

new(Name, Attack, Defense) -> #branch{name = Name, attack = Attack, defense = Defense}.

create(Name, Attack, Defense) -> 
	Branch = new(Name, Attack, Defense),
	active_record:create(Branch#branch{id = rand:new()}).

%% Tests

new_object_test_() ->
	Branch = new("Infantry", 4, 3),
	[
	?_assertMatch("Infantry", Branch#branch.name),
	?_assertMatch(4, Branch#branch.attack),
	?_assertMatch(3, Branch#branch.defense)
	].

creation_test_() ->
	[Before, After] = active_record:in_test_db(
		fun() ->
			Query = qlc:q([B || B <- mnesia:table(branch)]),
			BranchesBeforeCreate = active_record:find(Query),
			Branch = create("Infantry", 4, 3),
			BranchesAfterCreate = active_record:find(Query),
			[BranchesBeforeCreate, BranchesAfterCreate]
		end
	),
	[
	?_assertMatch({atomic, []}, Before),
	?_assertMatch({atomic, [{branch, _Id, "Infantry", 4, 3}]}, After)
	].